/**
 * @param {number[]} parents
 * @return {number}
 */
 var countHighestScoreNodes = function(parents) {
  const n = parents.length
  const g = new Map()
  for(let i = 0; i<n; i++){
      if(parents[i] === -1) continue
      if(!g.has(i)) g.set(i, [])
      if(!g.has(parents[i])) g.set(parents[i],[])
      g.get(i).push(parents[i])
      g.get(parents[i]).push(i)
  }
  const bfs = (base, now, cnt) => {
      const q = [now]
      const used = new Array(n).fill(false)
      used[base] = true
      used[now] = true
      while(q.length){
          for(let i = 0, len = q.length; i<len; i++){
              cnt++
              let t = q.shift()
              for(const next of g.get(t)){
                  if(used[next]) continue
                  used[next] = true
                  q.push(next)
              }
          }
      }
      return cnt
  }
  const scores = []
  for(let i = 0; i<n; i++){
      const child = []
      for(const next of g.get(i)){
          child.push(bfs(i, next, 0))
      }
      scores.push(child.reduce((pre, cur) => pre * cur, 1))
  }
  let max = Math.max(...scores)
  let ans = 0
  for(const x of scores){
      if(x === max) ans++
  }
  return ans
};